package
{
	import com.senocular.display.*;
	
	import flash.events.ProgressEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.controls.ProgressBar;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	import mx.controls.TileList;

	
	public class klborder extends Canvas
	{
		/**
		 * 原始数据
		 */
		private var _ori_width:Number; //宽
		private var _ori_height:Number; //高
		private var _ori_img_original:String; //原始图片的地址
		private var _ori_thumb_url:String; //缩累
		private var _ori_img_desc:String; //说明
		private var _ori_img_id:String; //id
		private var _ori_goods_id:String; //商品编号
		private var _ori_hotarea:String; //热区数据xml格式
		private var _ori_array:Object;
		
		private var _hotarea_info:Object;
		
		private var border1:Image;
		private var img:Image;
			
		public function klborder(args:Object)
		{
			//TODO: implement function
			super();
			
			this.verticalScrollPolicy = "off";
			this.horizontalScrollPolicy = "off";
			
			//保存原始数据
			this._ori_array = args;
			//ob1 = new ObjectHandles(this);
			
			border1 = new Image();
			addChild(border1);
			//this.setChildIndex(border1, this.numChildren-1);
			
			img = new Image();
			addChild(img);
			//this.setChildIndex(img, 0);
			
			addBorder();
			addHotarea();
			
			//this.addEventListener(ResizeEvent.RESIZE, _resizeme);
			
			
			//ha1.addEventListener(DragEvent.DRAG_ENTER,dragEnterHandler);
			//ha1.addEventListener(DragEvent.DRAG_DROP,dragDropHandler);
    				
		}
		
		/**
		 * 添加边框图片
		 * 满画布，宽高100%，随同父容器非等比例缩放
		 */
		private function addBorder():void{
			var tmp:XML = XML(this._ori_array);
			
			//添加border img
			//img = new Image();
			border1.percentWidth = 100;
			border1.percentHeight = 100;
			//img.addEventListener(ProgressEvent.PROGRESS, imgloadprogress);
			
			border1.maintainAspectRatio = false;
			//addChild(border1);
			this.width = tmp.width;
			this.height = tmp.height;
			border1.load(tmp.urlx);
		}//fm end
		
		/**
		 * 设置热区的位置
		 */
		private function addHotarea():void{
			var tmp:XML = XML(this._ori_array);
			
			//添加hotarea
			if(tmp.hotarea!=''){
				var t3:XML = XML(tmp.hot);
				//Alert.show(t3);
				for each(var ss:XML in t3.hoterea.info){
					this._hotarea_info = [
											{_name:'hotarea_1'},
											{_id:'hotarea_1'},
											{_width:ss.width},
											{_height:ss.height},
											{_rotation:ss.ratate},
											{_x:ss.x},
											{_y:ss.y}
										];	
					//ha1.name = 'hotarea'+ncs;
					//ha1.id = ha1.name;
					//ha1.width = ss.width;
					//ha1.height = ss.height;
					//ha1.rotation = ss.rotate;
					//ha1.x = ss.x;
					//ha1.y = ss.y;
					//ob1.registerComponent(ha1, ha1);
					//ha1.addEventListener(MouseEvent.CLICK, _x);
    				//ha1.addEventListener(MouseEvent.MOUSE_OVER, function (e:MouseEvent){e.currentTarget.setStyle("borderColor","#FF6633");e.currentTarget.setStyle("borderStyle","solid");e.currentTarget.setStyle("borderThickness","4"); });
					//ha1.addEventListener(MouseEvent.MOUSE_OUT, function (e:MouseEvent){e.currentTarget.setStyle("borderStyle","none");});
    				
    				//ha1.doubleClickEnabled = true;
    				//ha1.addEventListener(MouseEvent.DOUBLE_CLICK, function (e:MouseEvent){hotareadbclick(e, ha1);});
					//addChild(ha1);
					//Alert.show(ha1.width.toString());
				}
				
			}
			
			
			
		}//fn end
		
		/**
		 * 图片载入事件
		 */
		private function imgloadprogress(e:ProgressEvent):void{
				//var m:Matrix;
				//Alert.show(e.bytesLoaded+' / '+e.bytesTotal);
				//this.graphics.clear(); 
                //this.graphics.beginGradientFill("linear", [0x0000ff,0xffffff], [1, 1], [0x00, 0xff], m); 
                //this.graphics.drawRect(0, 0, (e.bytesLoaded / e.bytesTotal) * 300, 40); 
                //this.graphics.endFill();
                var pb1:ProgressBar = new ProgressBar();
                pb1.setProgress(e.bytesLoaded,e.bytesTotal);
                var j:Number = e.bytesLoaded / e.bytesTotal;
				pb1.label = "已加载："+j+"%";
                addChild(pb1);
		}//fn end
		
		// mainid的dragEnter事件
		private function dragEnterHandler(event:DragEvent):void {
    			DragManager.acceptDragDrop(Canvas(event.currentTarget));
		}//fn end
		
		//鼠标释放
		private  function dragDropHandler(event:DragEvent):void {
    			var tl:TileList = event.dragInitiator as TileList;
    			
				img.source = ((event.dragInitiator as TileList).selectedItem.urlx);
				img.width = this._hotarea_info._width;
				img.height = this._hotarea_info._height;
				img.x = this._hotarea_info._x;
				img.y = this._hotarea_info._y;
				
				//image.maintainAspectRatio = false;
				//img.id = 'mPhoto'+ncs;
				//image.name = 'mPhoto'+ncs;
				//ob.registerComponent(image, image);
				//if(/*(width>=this.width)||*/(image.height>=this.height)){
					//img.width = this.width;
					//image.height = this.height;
					//image.x = this.width/2 - image.width/2;
				//}else{
					//image.width = width;
					//image.height = height;
					//image.x = this.mouseX ;
					//image.y = this.mouseY;
				//}
				
				//this.addChild(image);
		}//fn end
		
	}
}